# 怪物脚本（自动寻路）
extends CharacterBody2D
@onready var collision_shape = $Area2D/CollisionShape2D

var speed : float = 150
var target_position : Vector2

var player_a

func _ready():
	pass

func initialize(spawn_pos, playerTmp):
	global_position = spawn_pos
	player_a = playerTmp
	speed = randf_range(120, 180)

func _physics_process(delta):
	
	# 计算移动方向
	var direction = (player_a.global_position - global_position).normalized()
	velocity = direction * speed
		# 可选：怪物朝向玩家旋转
	rotation = direction.angle()
	# 实现移动
	move_and_slide()

# 碰撞事件
func _on_area_2d_body_entered(body: Node2D) -> void:
	if body.is_in_group("player"):
		queue_free()
